﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Douban.Modules.Info.Views;
using Microsoft.Practices.ServiceLocation;
using Douban.Api;
using Douban.Api.User;
using Douban.Core;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.Windows.Input;
using System.Diagnostics;
using Douban.Api.Broadcast;
using Douban.Core.Helpers;
using Microsoft.Practices.Composite.Events;
using Douban.Core.Events;

namespace Douban.Modules.Info.ViewModels
{
    public class UserInfoViewModel : ObservableObject, IUserInfoViewModel
    {
        private UserDetail _user;
        private Miniblog _miniblog;
        private DateTime _latestMiniblogPublishedTime;
        private string _latestMiniblog;
        private IEventAggregator _eventAggregator;
        private ICommand _goToDoubanCommand;
        private ICommand _backCommand;
        private ICommand _sendMessageCommand;

        public UserInfoViewModel(IUserInfoView view, IEventAggregator eventAggregator)
        {
            View = view;
            View.Context = this;

            _eventAggregator = eventAggregator;
        }

        public IUserInfoView View { get; set; }

        public Miniblog Miniblog 
        {
            get { return _miniblog; }
            set
            {
                _miniblog = value;
                if (_miniblog != null)
                {
                    LatestMiniblogPublishedTime = _miniblog.PublishedTime;
                    LatestMiniblog = _miniblog.HtmlContent;
                }
                else
                {
                    LatestMiniblogPublishedTime = default(DateTime);
                    LatestMiniblog = "目前没有广播"; 
                }
            }
        }

        public UserDetail User 
        {
            get { return _user; }
            set 
            {
                if (_user != value)
                {
                    _user = value;
                    NotifyPropertyChanged("User");
                }
            }
        }

        public DateTime LatestMiniblogPublishedTime
        {
            get { return _latestMiniblogPublishedTime; }
            set
            {
                _latestMiniblogPublishedTime = value;
                NotifyPropertyChanged("LatestMiniblogPublishedTime");
            }
        }

        public string LatestMiniblog
        {
            get
            {
                return _latestMiniblog;
            }
            set
            {
                if (_latestMiniblog != value)
                {
                    _latestMiniblog = value;
                    NotifyPropertyChanged("LatestMiniblog");
                }
            }
        }

        public ICommand BackCommand 
        {
            get
            {
                if (_backCommand == null)
                    _backCommand = new DelegateCommand<object>(GoBack);

                return _backCommand;
            }
        }

        public ICommand GoToDoubanCommand 
        { 
            get 
            {
                if (_goToDoubanCommand == null)
                    _goToDoubanCommand = new DelegateCommand<object>(
                        obj => Process.Start(User.DoubanPage));

                return _goToDoubanCommand;
            } 
        }

        public ICommand SendMessageCommand
        {
            get 
            {
                if (_sendMessageCommand == null)
                    _sendMessageCommand = new DelegateCommand<object>(
                        obj =>
                        {
                            Debug.WriteLine(_user.Id);
                            _eventAggregator.GetEvent<ContactDoubleClickedEvent>().Publish(_user.Id);
                        });

                return _sendMessageCommand;
            }
        }

        private void GoBack(object parameter)
        {
            if (Back != null)
                Back(this, new EventArgs());
        }

        public event EventHandler Back;
    }
}
